#ifndef EDITOR_H
#define EDITOR_H
#include <vector>
#include <string>
#include "hge.h"
#include "hgegui.h"
#include "hgeguictrls.h"
#include <guichan.hpp>

using namespace gcn;
struct MapTile;
struct MapElem;
typedef std::list<MapElem*> MapElemList;
class Map;
class MapLayer;
struct Region;
class Entity;
class Scene;
struct EntityDef;
typedef std::list<Entity*> EntityList;

namespace Editor
{
	extern gcn::Gui *gui;
enum EDITORMODE
{
    EDITOR_TILE = 0,
    EDITOR_TERRAIN,
    EDITOR_REGION,
	EDITOR_UNIT,
};

struct EditState
{
    EditState()
    {
        curtile = NULL;
        screenx = 0;
        screeny = 0;
        curmap  = NULL;
        curlayer = NULL;
        drag = false;
        editormode = EDITOR_TILE;
        terraintype = 0;
        tileunitsize = 16;
        viewalllayer = true;
        curregion = NULL;
		curunit = NULL;
		scene = NULL;
    }
    float x;
    float y;
    float gamex;
    float gamey;
    float screenx;
    float screeny;
    MapTile *curtile;
	Entity *curunit;
	Scene *scene;
    Map *curmap;
    MapLayer *curlayer;
    Region *curregion;
    bool drag;
    gcn::Rectangle dragarea;
    MapElemList selectedElems;
	EntityList selectedUnits;
    int editormode;
    int terraintype;
    int tileunitsize;
    bool viewalllayer;
};
extern EditState edtstate;
struct Tile
{
    Tile()
    {
        sprite = NULL;
    }
    int tileid;
    std::string filename;
    hgeSprite *sprite;
};

typedef std::vector<Tile> MapTileVec;
extern MapTileVec alltilevec;


class TileModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
    MapTile *GetTileAt( int i );
};

class EditorModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
};

class TerrainModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
};

class UnitModelList:public gcn::ListModel
{
public:
	int getNumberOfElements();

	std::string getElementAt(int i);
	EntityDef *GetUnitDefAt( int i );
};

class MapModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
};

class LayerModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
};

class RegionModelList:public gcn::ListModel
{
public:
    int getNumberOfElements();

    std::string getElementAt(int i);
};

class TileSelectionEvent:public gcn::SelectionListener
{
public:
    virtual void valueChanged(const gcn::SelectionEvent& event);
};

class EditModeSelectionEvent:public gcn::SelectionListener
{
public:
    virtual void valueChanged(const gcn::SelectionEvent& event);
};

class MapSelectionEvent:public gcn::SelectionListener
{
public:
    virtual void valueChanged(const gcn::SelectionEvent& event);
};

class LayerSelectionEvent:public gcn::SelectionListener
{
public:
    virtual void valueChanged(const gcn::SelectionEvent& event);
};

class SaveButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class OpenButtonEvent:public gcn::MouseListener
{
public:
	virtual void mousePressed(MouseEvent& mouseEvent);
};

class AddMapButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class DeleteMapButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class AddLayerButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class DeleteLayerButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class ViewAllLayerButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class EditRegionButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};

class DelRegionButtonEvent:public gcn::MouseListener
{
public:
    virtual void mousePressed(MouseEvent& mouseEvent);
};
void handleScreenMove( float dt );
void handleSelectedElems( float dt );
void renderSelectedElems();
void renderSelectedUnits();
void renderGrid();
void renderTerrain();
void renderRegion();

bool renderFuncTile( );
bool renderFuncTerrain( );
bool renderFuncRegion( );
bool renderFuncUnit( );

bool frameFuncTile( float dt );
bool frameFuncTerrain( float dt );
bool frameFuncRegion( float dt );
bool frameFuncUnit( float dt );

extern TileModelList tilemodelist;
extern TerrainModelList terrainmodelist;
extern EditorModelList editormodelist;
extern RegionModelList regionmodelist;
extern UnitModelList unitmodelist;
}

#endif